package gemini.pollux.ui.client.page.content.product.product.list;

import gemini.basic.model.Product;
import gemini.basic.model.ProductType;
import gemini.pollux.ui.client.ServiceCatalog;
import gemini.pollux.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetDisplay;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetPresenter;
import gemini.pollux.ui.client.page.RootEvent;
import gemini.pollux.ui.client.page.content.marketingplan.level.LevelEvent;
import gemini.pollux.ui.client.page.content.product.product.ProductEvent;
import gemini.pollux.ui.client.page.content.product.product.ProductObject;
import gemini.pollux.ui.client.page.content.product.product.widget.ProductTableEvent;
import gemini.pollux.ui.client.page.content.product.product.widget.ProductTableHandler;
import gemini.pollux.ui.client.page.content.product.product.widget.ProductTableWidget;
import gemini.pollux.ui.client.utils.GuiUtils;
import gemini.pollux.ui.client.utils.UIConstants;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.inject.Inject;

public class ProductListPresenter extends
		PolluxWidgetPresenter<ProductListPresenter.Display> {

	@Inject
	public ProductListPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends PolluxWidgetDisplay {
		void showProducts(List<Product> products);

		ProductTableWidget getProductTableWidget();
		
		void setProductTypes(List<ProductType> productTypes);
	}

	public static final Place PLACE = new Place(UIConstants.PLACE_PRODUCT);
	private ProductObject outputObject;
	
	
	@Override
	protected void onBind() {
		registerHandler(display.getProductTableWidget().addProductActionHandler(new ProductTableHandler() {
			
			@Override
			public void onEdit(ProductTableEvent event) {
				outputObject = new ProductObject();
				outputObject.setProduct(event.getProduct())	;
				
				localEventBus.fireEvent(new ProductEvent(ProductEvent.ForwardType.EDIT));
			}
			
			@Override
			public void onDelete(ProductTableEvent event) {
				eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
				ServiceCatalog.getLevelService().deleteLevel(event.getProduct().getId(), 
						new AbstractAsyncCallback<Void>() {

							@Override
							public void onSuccess(Void result) {
								localEventBus.fireEvent(new LevelEvent(LevelEvent.ForwardType.BACK_TO_LIST));
							}
							
							@Override
							public void onFailure(final Throwable caught) {
								GuiUtils.alert("This Level is in used! Cannot delete!");
							}
						});
			}
			
			@Override
			public void onAdd(ProductTableEvent event) {
				localEventBus.fireEvent(new ProductEvent(ProductEvent.ForwardType.NEW));
			}
		}));
		
		loadProducts();
	}
	
	private void loadProducts() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getProductService().getAllProducts(
				new AbstractAsyncCallback<ArrayList<Product>>() {

					@Override
					public void onSuccess(ArrayList<Product> result) {
						eventBus.fireEvent(
								new RootEvent(RootEvent.ForwardType.UN_MASK));
						
						display.setProductTypes(productTypes);
						display.initCustom();
						display.showProducts(result);
					}
				});
	}
	
	private List<ProductType> productTypes;
	
	@Override
	public void setInputObject(Object ob){
		ProductObject lvObject = (ProductObject) ob;
		if (lvObject != null) {
			this.productTypes = lvObject.getProductTypes();
		}
	}


	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	@Override
	public Object getOutputObject(){
		return this.outputObject;
	}
}
